fix(db): prevent duplicate member segment affiliations#3985
Open
prembharne wants to merge 4 commits intolinuxfoundation:mainfrom
Open
fix(db): prevent duplicate member segment affiliations#3985prembharne wants to merge 4 commits intolinuxfoundation:mainfrom
prembharne wants to merge 4 commits intolinuxfoundation:mainfrom
Conversation
Adds a pre-insert SELECT check in TypeScript and a concrete UNIQUE index migration on memberSegmentAffiliations robustly resolving duplicate affiliation logging problems. Signed-off-by: Prem bharne <prembharne455@gmail.com>
Signed-off-by: Prem bharne <prembharne455@gmail.com>
backend/src/database/repositories/memberSegmentAffiliationRepository.ts
Outdated
Show resolved
Hide resolved
backend/src/database/migrations/V1775000000__fix-member-segment-affiliations.sql
Show resolved
Hide resolved
Eliminates race condition under READ COMMITTED isolation and fixes COALESCE sentinel mismatch between the unique index and the application-level query. Signed-off-by: Prem bharne <prembharne455@gmail.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
backend/src/database/migrations/V1775000000__fix-member-segment-affiliations.sql
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #2840
Description:
This PR introduces a strict deduplication migration for the memberSegmentAffiliations table and enforces a UNIQUE index that handles NULL date values natively using COALESCE.
Additionally, we added a robust pre-insert SELECT check in memberSegmentAffiliationRepository.ts's createOrUpdate function. This prevents unhandled 500 Constraint Errors and updates seamlessly when exact matches are triggered, ensuring consistency across segments.
Changes:
Added .sql cleanup and indexing migration.
Graceful TypeScript fail-safes in backend integration preventing blind inserts.
Note
Medium Risk
Adds a data-cleanup migration and a new uniqueness constraint on
memberSegmentAffiliations, which could impact production data and fail if unexpected duplicates/NULL patterns exist. Also changes insert behavior to upsert on conflicts, affecting write semantics for this table.Overview
Cleans up the
memberSegmentAffiliationstable by deleting exact duplicate rows (same member/segment/org/dateStart/dateEnd) and then enforces a new unique index that treatsNULLvalues as equal viaCOALESCE.Updates
MemberSegmentAffiliationRepository.createOrUpdateto useINSERT ... ON CONFLICTagainst that same composite key (including theCOALESCEexpressions), turning duplicate inserts into an upsert instead of raising a constraint error.Written by Cursor Bugbot for commit 78f029e. This will update automatically on new commits. Configure here.